package com.globallogic.restdemo.utils;

import com.globallogic.restdemo.dto.User;

import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.ValidatorFactory;
import java.util.Set;

public class UserValidator implements Validator<User> {


    private static ValidatorFactory vf = Validation.buildDefaultValidatorFactory();
    private javax.validation.Validator validator;

    public UserValidator(){
        validator = vf.getValidator();
    }

    @Override
    public boolean isValid(User bean) {
        Set<ConstraintViolation<User>> constraintViolations = validator
                .validate(bean);

        for (ConstraintViolation<User> cv : constraintViolations)
            System.out.println(String.format(
                    "Внимание, ошибка! property: [%s], value: [%s], message: [%s]",
                    cv.getPropertyPath(), cv.getInvalidValue(), cv.getMessage()));

        return constraintViolations.isEmpty();
    }
}
